package team1.hdhnewsapp.newsactivity;

import info.androidhive.slidingmenu.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class SportRSSFragment extends Fragment {
	ArrayList<HashMap<String, String>> rssItemList = new ArrayList<HashMap<String, String>>();
	ListView lv3;
	View view;
	RSSParser rssParser = new RSSParser();
	private static String TAG_TITLE = "title";
	private static String TAG_LINK = "link";
	private static String TAG_DESRIPTION = "description";
	private static String TAG_PUB_DATE = "pubDate";
	List<RSSItem> rssItems = new ArrayList<RSSItem>();
	String[] test = new String[] { TAG_LINK, TAG_TITLE, TAG_PUB_DATE,
			TAG_DESRIPTION };

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_sportrssfragment,
				container, false);
		lv3 = (ListView) rootView.findViewById(R.id.list);
		lv3.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent in = new Intent(getActivity(),
						DisPlayWebPageActivity.class);

				// getting page url
				String page_url = rssItems.get(arg2).getLink();
				in.putExtra("page_url", page_url);
				startActivity(in);
			}
		});
		;
		new PostTask().execute("http://vnexpress.net/rss/the-thao.rss");
		rssItemList.clear();
		return rootView;
	}

	private class PostTask extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			String url1 = params[0];
			// list of rss items
			rssItems = rssParser.getRSSFeedItems(url1);

			// looping through each item
			for (RSSItem item : rssItems) {
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();

				// adding each child node to HashMap key => value
				map.put(TAG_TITLE, item.getTitle());
				map.put(TAG_LINK, item.getLink());
				map.put(TAG_PUB_DATE, item.getPubdate()); // If you want parse
															// the date
				String description = item.getDescription();
				// taking only 200 chars from description
				if (description.length() > 100) {
					description = description.substring(0, 97) + "..";
				}
				map.put(TAG_DESRIPTION, description);

				// adding HashList to ArrayList
				rssItemList.add(map);
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			/**
			 * Updating parsed items into listview
			 * */
			ListAdapter adapter = new SimpleAdapter(getActivity(), rssItemList,
					R.layout.rss_item_list_row, test, new int[] {
							R.id.page_url, R.id.title, R.id.pub_date });
			// updating listview
			lv3.setAdapter(adapter);
		}
	}

	public InputStream getInputStream(URL url) {
		try {
			return url.openConnection().getInputStream();
		} catch (IOException e) {
			return null;
		}
	}
}
